package com.combetter.changxing.ui.dialog;

import android.bluetooth.BluetoothGatt;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import com.clj.fastble.BleManager;
import com.jakewharton.rxbinding2.view.RxView;
import com.combetter.changxing.MyApp;
import com.combetter.changxing.R;
import com.combetter.changxing.bluetooth.FacilityClient2;
import com.combetter.changxing.databinding.DialogExitBinding;
import com.combetter.changxing.db.entity.User;
import com.combetter.changxing.ui.activity.LoginActivity;
import com.combetter.changxing.util.AppManager;
import com.combetter.changxing.util.ResUtils;
import com.combetter.changxing.util.SharePreferencesUtils;

import cn.jpush.android.api.JPushInterface;

import static com.combetter.changxing.bluetooth.FacilityClient2.AUTH_NONE;

/**
 * 退出app
 * <p>
 * Created by 深圳唐龙飞讯科技有限公司 on 2018/7/3.
 */
public class ExitDialog extends DialogFragment {
    private DialogExitBinding exitBinding;
    SharePreferencesUtils utils = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        exitBinding = DataBindingUtil.inflate(inflater, R.layout.dialog_exit, container, false);
        getDialog().setCanceledOnTouchOutside(false); // 外部点击取消
        utils = new SharePreferencesUtils(getActivity());
        initVies();
        return exitBinding.getRoot();
    }

    @Override
    public void onResume() {
        super.onResume();
        int width = ResUtils.getDimens(getContext(), R.dimen.DIMEN_550PX);
        int height = ResUtils.getDimens(getContext(), R.dimen.DIMEN_250PX);
        getDialog().getWindow().setLayout(width, height);


    }

    /**
     * 初始化控件
     */
    private void initVies() {
        User user = MyApp.getInstance().getUserInfo();
        RxView.clicks(exitBinding.tvCancel).subscribe(o -> dismiss());
        RxView.clicks(exitBinding.tvConfirm).subscribe(o -> {
            if (FacilityClient2.mConnectState == BluetoothGatt.STATE_CONNECTED) {
                BleManager.getInstance().disconnectAllDevice();
                FacilityClient2.getClient().disconnect();
                FacilityClient2.mAuthStatu = AUTH_NONE;
            }
            MyApp.getInstance().getDatabase().userDao().deleteUser(user);
            MyApp.getInstance().getDatabase().collectDao().deleteAll();
            SharePreferencesUtils utils = new SharePreferencesUtils(MyApp.getInstance());
            utils.clearMainHistory();
            utils.clearOtherHistory();
            utils.clearLastDevice();
            startActivity(new Intent(getActivity(), LoginActivity.class));
            if (!JPushInterface.isPushStopped(getActivity())) {
                JPushInterface.onPause(getActivity());
                JPushInterface.stopPush(getActivity());
            }
            AppManager.finishAllActivity();
        });
    }
}
